<template>
  <el-main>
    <!-- 搜索栏 -->
    <el-form ref="searchForm" :model="searchForm" :inline="true" size="small">
      <el-form-item prop="courseName">
        <el-input v-model="searchForm.courseName" placeholder="请输入课程名称" />
      </el-form-item>
      <el-form-item>
        <el-button icon="el-icon-search" @click="searchBtn">搜索</el-button>
        <el-button style="color:#FF7670;" icon="el-icon-close" @click="resetBtn">重置</el-button>
        <el-button v-permission="['sys:courseList:add']" type="primary" icon="el-icon-plus" @click="addBtn">新增</el-button>
      </el-form-item>
    </el-form>
    <!-- 新增弹框 -->
    <sys-dialog
      :title="addDialog.title"
      :height="addDialog.height"
      :width="addDialog.width"
      :visible="addDialog.visible"
      @onClose="onClose"
      @onConfirm="onConfirm"
    >
      <div slot="content">
        <el-form ref="addModel" :model="addModel" :rules="rules" label-width="80px" :inline="false" size="small">
          <el-form-item prop="courseName" label="课程名称">
            <el-input v-model="addModel.courseName" />
          </el-form-item>

          <el-form-item prop="courseDesc" label="课程描述">
            <el-input v-model="addModel.courseDesc" />
          </el-form-item>

        </el-form>
      </div>
    </sys-dialog>
    <!-- 分配弹框 -->
    <sys-dialog
      :title="assignDialog.title"
      :height="assignDialog.height"
      :width="assignDialog.width"
      :visible="assignDialog.visible"
      @onClose="onCloseAssign"
      @onConfirm="onConfirmAssign"
    >
      <div slot="content">
        <el-transfer
          v-model="value"
          filterable
          :filter-method="filterMethod"
          filter-placeholder="请输入老师姓名"
          :props="{
            key: 'teacherId',
            label: 'teacherName'
          }"
          :data="assignList"
          @change="btnChange"
          @left-check-change="leftChange"
        />
      </div>
    </sys-dialog>
    <!-- 课程表格列表 -->
    <el-table :data="tableList" :height="tableHeight" border stripe>
      <el-table-column prop="courseName" label="课程名称" />
      <el-table-column prop="courseDesc" label="课程描述" />
      <el-table-column prop="teacherName" label="授课老师">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.teacherId" type="success" size="small">{{ scope.row.teacherName }}</el-tag>
          <el-tag v-else type="danger" size="small">未分配</el-tag>

        </template>
      </el-table-column>
      <el-table-column v-if="$checkPermission(['sys:courseList:edit', 'sys:courseList:assign', 'sys:courseList:remove', 'ssys:courseList:delete'])" label="操作" align="center" width="420">
        <template slot-scope="scope">
          <el-button v-permission="['sys:courseList:edit']" type="primary" size="small" icon="el-icon-edit" @click="editBtn(scope.row)">修改</el-button>
          <el-button v-permission="['sys:courseList:assign']" type="success" size="small" icon="el-icon-edit" @click="assignBtn(scope.row)">分配教师</el-button>
          <el-button v-if="scope.row.teacherId" v-permission="['sys:courseList:remove']" type="warning" size="small" icon="el-icon-edit" @click="removeBtn(scope.row)">解除教师</el-button>
          <el-button v-permission="['sys:courseList:delete']" type="danger" size="small" icon="el-icon-close" @click="deleteBtn(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页插件 -->
    <el-pagination
      :current-page.sync="searchForm.currentPage"
      :page-sizes="[10, 20, 40, 80, 100]"
      :page-size="searchForm.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="searchForm.total"
      background
      @size-change="sizeChange"
      @current-change="currentChange"
    />

  </el-main>

</template>

<script>
import SysDialog from '@/components/dialog/SysDialog.vue'
import { addCourseApi, assignSaveApi, deleteCourseApi, editCourseApi, getCourseListApi, removeTeacherApi } from '@/api/course'
import { getAssignTeacherListApi } from '@/api/teacher'
export default {
  components: {
    SysDialog
  },
  data() {
    return {
      value: [],
      // 穿梭框数据
      assign: {
        courseId: '',
        teacherId: ''
      },
      // 搜索参数设置
      searchForm: {
        currentPage: 1,
        pageSize: 10,
        total: 0,
        courseName: ''
      },
      assignDialog: {
        title: '',
        height: 300,
        width: 630,
        visible: false
      },
      // 弹出框设置
      addDialog: {
        title: '',
        height: 150,
        width: 460,
        visible: false
      },
      // 绑定弹出框数据
      addModel: {
        courseId: '',
        courseName: '',
        courseDesc: '',
        type: ''
      },
      // 弹出框表单数据验证
      rules: {
        courseName: [{
          required: true,
          message: '请输入课程名称',
          trigge: 'blur'
        }]
      },
      // 表格属性
      tableHeight: 0,
      // 表格数据
      tableList: [],
      // 分配教师列表
      assignList: []
    }
  },
  created() {
    this.getList()
  },
  mounted() {
    this.$nextTick(() => {
      this.tableHeight = window.innerHeight - 220
    })
  },
  methods: {
    async removeBtn(row) {
      const confirm = await this.$myconfirm('确认解除该课程老师吗')
      if (confirm) {
        const res = await removeTeacherApi({ teacherId: row.teacherId, courseId: row.courseId })
        if (res && res.code === 200) {
          this.$message.success(res.msg)
          this.getList()
        }
      }
    },
    btnChange(select, index) {
      const item = this.value
      if (item.length > 0) {
        for (let i = 0; i < item.length; i++) {
          for (let j = 0; j < select.length; j++) {
            if (item[i] !== select[j]) {
              item.splice(i, 1)
            }
          }
        }
      }
    },
    leftChange(item, index) {
      if (item.length > 0) {
        for (let i = 0; i < item.length; i++) {
          for (let j = 0; j < index.length; j++) {
            if (item[i] !== index[j]) {
              item.splice(i, 1)
            }
          }
        }
      }
    },
    filterMethod(query, item) {
      return item.teacherName.indexOf(query) > -1
    },
    // 页容量变化
    sizeChange(val) {
      this.searchForm.pageSize = val
      this.getList()
    },
    // 当前页变化
    currentChange(val) {
      this.searchForm.currentPage = val
      this.getList()
    },
    // 搜索按钮
    searchBtn() {
      this.getList()
    },
    // 重置按钮
    resetBtn() {
      this.searchForm.courseName = ''
      this.searchForm.currentPage = 1
      this.searchForm.pageSize = 10
      this.getList()
    },
    // 新增按钮
    addBtn() {
      // 清空数据
      if (this.$refs.addModel !== undefined) {
        this.$refs.addModel.resetFields()
        this.$emptyData(this.addModel)
      }
      this.addDialog.title = '新增课程信息'
      this.addModel.type = '0'
      this.addDialog.visible = true
    },
    onClose() {
      this.addDialog.visible = false
    },
    onCloseAssign() {
      this.assignDialog.visible = false
    },
    onConfirm() {
      this.$refs.addModel.validate(async(valid) => {
        if (valid) {
          let res = null
          if (this.addModel.type === '0') {
            res = await addCourseApi(this.addModel)
          } else {
            res = await editCourseApi(this.addModel)
          }
          if (res && res.code === 200) {
            // 消息提示
            this.$message.success(res.msg)
            // 刷新列表
            this.getList()
            // 关闭弹窗
            this.addDialog.visible = false
          }
        }
      })
    },
    async onConfirmAssign() {
      if (this.value.length === 0) {
        this.$message.warning('请选择教师')
        return
      }
      this.assign.teacherId = this.value[0]
      // console.log(this.assign)
      const res = await assignSaveApi(this.assign)
      if (res && res.code === 200) {
        this.$message.success(res.msg)
        this.getList()
        this.assignDialog.visible = false
      }
    },
    // 分配教师
    async assignBtn(row) {
      this.value = []
      this.assign.courseId = row.courseId
      // 查询教师列表
      const res = await getAssignTeacherListApi()
      if (res && res.code === 200) {
        this.assignList = res.data
      }
      this.assignDialog.title = '为【' + row.courseName + '】' + '分配教师'
      this.assignDialog.visible = true
    },
    editBtn(row) {
      // 清空数据
      if (this.$refs.addModel !== undefined) {
        this.$refs.addModel.resetFields()
        this.$emptyData(this.addModel)
      }
      this.$objCopy(row, this.addModel)
      this.addDialog.title = '编辑课程信息'
      this.addModel.type = '1'
      this.addDialog.visible = true
    },
    async deleteBtn(row) {
      const confirm = await this.$myconfirm('确认删除该条数据吗')
      if (confirm) {
        const res = await deleteCourseApi({ courseId: row.courseId })
        if (res && res.code === 200) {
          this.$message.success(res.msg)
          this.getList()
        }
      }
    },
    async getList() {
      const res = await getCourseListApi(this.searchForm)
      if (res && res.code === 200) {
        this.tableList = res.data.records
        this.searchForm.total = res.data.total
      }
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
